#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

u8 RM195_otptime = 0;

/*RM67195 Gamma寄存器位址*/
u8 RM195_Gamma_r[62]=
{       
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2D,0x2F,0x30,0x31,
    0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3D,0x3F,0x40,0x41
};
u8 RM195_Gamma_g[62]=
{
    0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,
    0x52,0x53,0x54,0x55,0x56,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,
    0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
    0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,0x80
};
u8 RM195_Gamma_b[62]=
{
    0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,
    0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xA0,
    0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,
    0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,0xC0
};

//----------------------------------------------------------------------------------
/*RM67295 Gamma寄存器位址*/
u8 RM295_Gamma_r[62]=
{       
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x30,0x31,0x32,0x33,
	0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x40,0x41,0x42,0x43,0x44,0x45
};
u8 RM295_Gamma_g[62]=
{
    0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x58,0x59,
    0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
    0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
    0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87
};
u8 RM295_Gamma_b[62]=
{
    0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
    0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xA4,0xA5,0xA6,0xA7,0xAC,0xAD,0xAE,0xAF,
    0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
    0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xc6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD
};
//----------------------------------------------------------------------------------
/*RM67198 Gamma寄存器位址*/
u8 RM198_Gamma33pt_r[66]=
{       
    0x00,0x01,0xCE,0xCF,0xD0,0xD1,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
    0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,
    0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,
    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x40,0x41,0x42,0x43,
    0x44,0x45
};
u8 RM198_Gamma33pt_g[66]=
{
    0x46,0x47,0xD2,0xD3,0xD4,0xD5,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,
    0x52,0x53,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,0x63,0x64,0x65,
    0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,
    0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,0x80,0x81,0x82,0x83,0x84,0x85,
    0x86,0x87
};
u8 RM198_Gamma33pt_b[66]=
{
    0x88,0x89,0xD6,0xD7,0xD8,0xD9,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,
    0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xA4,0xA5,0xA6,0xA7,
    0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,
    0xBC,0xBD,0xBE,0xBF,0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xc6,0xC7,0xC8,0xC9,0xCA,0xCB,
    0xCC,0xCD
};
//----------------------------------------------------------------------------------
/*RM67198 Gamma寄存器位址*/
u8 RM198_Gamma19pt_r[38]=
{       
    0x00,0x01,0xD0,0xD1,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
    0x10,0x11,0x14,0x15,0x16,0x17,0x1A,0x1B,0x1E,0x1F,0x22,0x23,0x28,0x29,
    0x34,0x35,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45
};
u8 RM198_Gamma19pt_g[38]=
{
    0x46,0x47,0xD4,0xD5,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,
    0x5A,0x5B,0x5E,0x5F,0x60,0x61,0x64,0x65,0x68,0x69,0x6C,0x6D,0x72,0x73,
    0x7A,0x7B,0x7E,0x7F,0x82,0x83,0x84,0x85,0x86,0x87
};
u8 RM198_Gamma19pt_b[38]=
{
    0x88,0x89,0xD8,0xD9,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,
    0x98,0x99,0x9C,0x9D,0x9E,0x9F,0xA6,0xA7,0xAE,0xAF,0xB2,0xB3,0xB8,0xB9,
    0xC0,0xC1,0xC4,0xC5,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,
};

/********************************************************************************************
********************************************************************************************
    RM67195、RM67295、RM67198  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67195、RM67295、RM67198  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67195、RM67295、RM67198  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67195、RM67295、RM67198  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67195、RM67295、RM67198  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void RM6719X_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
				SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    if(buffer[0]==RM67195)
    {
        SSD2828_W_Reg(SigMode,channel,0xBF,0xAAFB); 
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok
}

void RM6719X_Write51_Register(u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    if(buffer[0]==RM67195)
    {
        SSD2828_W_Reg(SigMode,channel,0xBF,0xAAFB); 
    }
}

void RM6719X_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[2];
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
        SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
    else
        SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
    buffer1[0] = 0x01;
    buffer1[1] = buffer[3];
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(20);
    buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok  
}

void RM6719X_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = ((buffer[0]==RM67198)||(buffer[0]==RM67298)) ? 0x50 : 0x05;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		if(buffer[0]==RM67198) //67198高亮gamma 需切至fe,51
		{
				if(buffer[3]&0x08)
				{
					buffer1[2]=0x51;
					SSD2828_W_Array(SigMode,channel,buffer1,0);
					buffer[3]-=0x08;
				}
		}
		if(buffer[0]==RM67298) //69298高亮gamma 需切至fe,51
		{
				if(buffer[3]&0x08)
				{
					buffer1[2]=0x52;
					SSD2828_W_Array(SigMode,channel,buffer1,0);
					buffer[3]-=0x08;
				}
		}
		  
    if((buffer[0]==RM67195)||(buffer[0]==RM67295)||(buffer[0]==RM67198)||(buffer[0]==RM67298))
    {
        for( i = 0;i<buffer[2]-1;i++) 
        {	 
            if(buffer[0]==RM67195)
            {
                if(buffer[3]==0x00)	
                    SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM195_Gamma_r[i]);  
                else if(buffer[3]==0x42)
                    SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM195_Gamma_g[i]); 
                else if(buffer[3]==0x81)
                    SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM195_Gamma_b[i]); 
                SSD2828_W_Reg(SigMode,channel,0xBF,0xAAFB); 
            }
            else if(buffer[0]==RM67295)
            {
                if(buffer[3]==0x00)	
                    SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM295_Gamma_r[i]);  
                else if(buffer[3]==0x42)
                    SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM295_Gamma_g[i]); 
                else if(buffer[3]==0x81)
                    SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM295_Gamma_b[i]); 																		
            }
            else if(buffer[0]==RM67198||(buffer[0]==RM67298))
            {
                if(buffer[2]==67)
                {
                    if(buffer[3]==0x00)	
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM198_Gamma33pt_r[i]);  
                    else if(buffer[3]==0x42)
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM198_Gamma33pt_g[i]); 
                    else if(buffer[3]==0x81)
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM198_Gamma33pt_b[i]); 																				
                }
                else if(buffer[2]==39)
                {
                    if(buffer[3]==0x00)	
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM198_Gamma19pt_r[i]);  
                    else if(buffer[3]==0x42)
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM198_Gamma19pt_g[i]); 
                    else if(buffer[3]==0x81)
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM198_Gamma19pt_b[i]); 																				
                }
                else
                {
                    if(buffer[3]==0x00)	
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM295_Gamma_r[i]);  
                    else if(buffer[3]==0x42)
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM295_Gamma_g[i]); 
                    else if(buffer[3]==0x81)
                        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM295_Gamma_b[i]); 																							
                }
                    
            }
            delay_ms(5);
        }
    }
    if(buffer[0]==RM67198||(buffer[0]==RM67298))
    {
        SSD2828_W_Reg(SigMode,channel,0xBF,0x00FE); 
    }				
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}

void RM6719X_auto_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    //SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
    for( i = 0;i<buffer[2];i++) 
    {	 
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|buffer[i+3]);  
            if(buffer[0]==RM67195)
            {
                    SSD2828_W_Reg(SigMode,channel,0xBF,0xAAFB);
            }
            i++;
            delay_ms(2);
    }				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void RM6719X_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(2);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = ((buffer[0]==RM67198)||(buffer[0]==RM67298)) ? 0x50 : 0x05;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
	
    if(buffer[0]==RM67198) //67198高亮gamma 需切至fe,51
    {
        if((buffer[3]&0x08)!=0)
        {
            buffer1[2]=0x51;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
            buffer[3]-=0x08;
        }
    }
		if(buffer[0]==RM67298) //67198高亮gamma 需切至fe,52
    {
        if((buffer[3]&0x08)!=0)
        {
            buffer1[2]=0x52;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
            buffer[3]-=0x08;
        }
    }
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
        SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
    else
        SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);      
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
    for( i = 0;i<buffer[2]-1;i++)			 	
    {  	
        
        SSD2828_W_Cmd(SigMode,channel,0xBF);
        if(buffer[0]==RM67195)
        {
            SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? RM195_Gamma_r[i] : ((buffer[3]==0x42) ? RM195_Gamma_g[i] : RM195_Gamma_b[i]));
            delay_ms(15); 
        }
        else if(buffer[0]==RM67295)
        {
            SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? RM295_Gamma_r[i] : ((buffer[3]==0x42) ? RM295_Gamma_g[i] : RM295_Gamma_b[i]));
            delay_ms(15); 
        }
        else if(buffer[0]==RM67198||(buffer[0]==RM67298))
        {
            if(buffer[2]==39)
            {
                SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? RM198_Gamma19pt_r[i] : ((buffer[3]==0x42) ? RM198_Gamma19pt_g[i] : RM198_Gamma19pt_b[i]));
            }
            else if(buffer[2]==67)
            {
                SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? RM198_Gamma33pt_r[i] : ((buffer[3]==0x42) ? RM198_Gamma33pt_g[i] : RM198_Gamma33pt_b[i]));
            }
            else
            {
                SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? RM295_Gamma_r[i] : ((buffer[3]==0x42) ? RM295_Gamma_g[i] : RM295_Gamma_b[i]));
            }
            delay_ms(8); 
        }
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);                                              
        delay_ms(12);                                                          
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF); 
    }
    delay_ms(1);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  
}


void RM67298_Demura_Ram_EN(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data,u8 RM298_EN)
{
		u8 Demura_EN_code_table[]=
		{
				0xfe,0x40,
				0x0e,0x80,
				0xfe,0x20,
				0xb1,0x00,
				0xfe,0xd0,
				0x55,0x30,
				0x07,0x01,
				0xfe,0x40,
				0x0e,0x50,
				0xfe,0xd0,
				0x42,0x81,
				0x27,0x55
		};
		u8 Demura_OFF_code_table[]=
		{
				0xfe,0x40,
				0x0e,0x80,
				0xfe,0x20,
				0xb1,0x00,
				0xfe,0xd0,
				0x55,0x00,
				0x07,0x01,
				0xfe,0x40,
				0x0e,0x50,
				0xfe,0xd0,
				0x42,0x01,
				0x27,0x55
		};		
		
		u8 buffer1[3];
	  u8 i=0;
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);
		for(i=0;i<24;i++)
		{
			buffer1[0] = 0x02;
			if(RM298_EN==1)
			{
					buffer1[1] = Demura_EN_code_table[i];
					buffer1[2] = Demura_EN_code_table[i+1];
			}
			else
			{
					buffer1[1] = Demura_OFF_code_table[i];
					buffer1[2] = Demura_OFF_code_table[i+1];		
			}
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			i+=2;
		}
					SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void RM6719X_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data,u8 RM195_otptime)
{
//    u16 tmp;
    u8 buffer1[3];
    delay_ms(120); 

    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
    delay_ms(15);
		
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(5);
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(200);  //otp
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x04;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    if((buffer[0]==RM67198)||(buffer[0]==RM67298))
    {															
        SSD2828_W_Reg(SigMode,channel,0xBF,0x40FE);	
    }
    delay_ms(200);  

    if(buffer[1]==0x0D)  //page0 RM67198&RM67195一致
		{
			if(buffer[0]==RM67298)
			 {
				 SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x01F6 : ((RM195_otptime==0x02) ? 0x01F7 : 0x80F9));//OTP_cmd2_P0_PGM_EN
			 }			 
			 else
			 {
        SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x01F7 : ((RM195_otptime==0x02) ? 0x01F8 : 0x02F8));//OTP_cmd2_P0_PGM_EN
       }
		 }			 
		if(buffer[1]==0x0A)  //page1
		 {
        if(buffer[0]==RM67198)
        {
            SSD2828_W_Reg(SigMode,channel,0xBF, 0x02F7);//OTP_cmd2_P1_PGM_EN
        }
				else if(buffer[0]==RM67298)
        {
            SSD2828_W_Reg(SigMode,channel,0xBF, 0x02F6);//OTP_cmd2_P1_PGM_EN
        }
        else
        {
            SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x02F7 : ((RM195_otptime==0x02) ? 0x08F8 : 0x10F8) );//OTP_cmd2_P1_PGM_EN
        }
     }
    if(buffer[1]==0x0E) //page 2 
		{		
				if(buffer[0]==RM67198)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x04F7 : 0x04F8);//OTP_cmd2_P2_PGM_EN
				}	
				else if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x04F6 : 0x04F7);//OTP_cmd2_P2_PGM_EN
				}	
				else 
        {
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x04F7 : ((RM195_otptime==0x02) ? 0x20F8 : 0x40F8));//OTP_cmd2_P2_PGM_EN
				}
		}
    if(buffer[1]==0x0F) //Page 3
		{
				if(buffer[0]==RM67198)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x80F7 : 0x08F8);//CGM_PGM_EN
				}	
				else if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x08F6 : 0x08F7);//OTP_cmd2_P3_PGM_EN
				}				
				else
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x38F7 : 0x0000);//OTP_cmd2_P3-5_PGM_EN
				}
		}
		
		if(buffer[1]==0x07)
		{
				if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x20F6 : 0x20F7);//SPR_PGM_EN
				}	
		}
		if(buffer[1]==0x17)//OTP_cmd3_ANA_PGM_EN
		{
				SSD2828_W_Reg(SigMode,channel,0xBF,0xD0FE);	//切换页
				if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x01E5 : 0x01E4);//OTP_cmd3_ANA_PGM_EN
				}	
				SSD2828_W_Reg(SigMode,channel,0xBF,0x40FE);	//切换页
		}
		if(buffer[1]==0x18)//OTP_Page_D0
		{
				SSD2828_W_Reg(SigMode,channel,0xBF,0xD0FE);	//切换页
				if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x02E5 : 0x02E4);//OTP_Page_D0
				}	
				SSD2828_W_Reg(SigMode,channel,0xBF,0x40FE);	//切换页
		}
		if(buffer[1]==0x19)//OTP_Page_E0
		{
				if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x40F6 : 0x40F7);//OTP_Page_E0
				}	

		}
		if(buffer[1]==0x1A)//OTP_Page_10
		{
				if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x80F6 : 0x80F7);//OTP_Page_10
				}	
		}
		if(buffer[1]==0x1B)//OTP_Page_12
		{
				if(buffer[0]==RM67298)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM195_otptime==0x01) ? 0x01F8 : 0x01F9);//OTP_Page_12
				}	
		}	
    delay_ms(200); 
    SSD2828_W_Reg(SigMode,channel,0xBF,0x03F2);
    SSD2828_W_Reg(SigMode,channel,0xBF,0xA5F3);
    SSD2828_W_Reg(SigMode,channel,0xBF,0x5AF4);
    SSD2828_W_Reg(SigMode,channel,0xBF,0x3CF5);                        
    delay_ms(800);         // wait for >400 ms   

    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		delay_ms(5); 
		
	/*	
		
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer1[0] = 0x01;
    buffer1[1] = 0xEF;
    SSD2828_W_Array(SigMode,channel,buffer,0);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   烧录OTP
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    delay_ms(100);															   								
    tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    if (tmp&0x02)       //出错
    {
        buffer[4] = Uart_Error_Oth;
    }
    else                //正确
			
	*/	
    buffer[4] = Uart_Error_None; 
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写OTP状态  ：ok
}

void RM6719X_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    u16 tmp;
    u8 RM195_otptime;
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x04;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    if(buffer[0]==RM67198)
    {															
        SSD2828_W_Reg(SigMode,channel,0xBF,0x40FE);
        SSD2828_W_Reg(SigMode,channel,0xBF,0x80F2); //otp_load_en	
    }															
    SSD2828_W_Reg(SigMode,channel,0xBF,0x03F6);
    
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
        SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
    else
        SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 

    delay_ms(5);
    buffer1[0] = 0x01;
    buffer1[1] = 0xEE;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);
                                        
    tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 

    if(buffer[0]==RM67198)	
    {
        if (tmp&0x10)
                RM195_otptime=4;
        else if (tmp&0x08)
                RM195_otptime=3;
        else if (tmp&0x04)
                RM195_otptime=2;
        else
        {
            buffer[0] = 0x01;
            buffer[1] = 0xED;
            SSD2828_W_Array(SigMode,channel,buffer,0);
            delay_ms(20);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
            delay_ms(5);
            tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 												
            if (tmp&0x02)
                RM195_otptime=1;																	
        }
    }
    else
    {
        if (tmp&0x10)
            RM195_otptime=3;
        else if (tmp&0x08)
            RM195_otptime=2;
        else
        {
            SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
            SSD2828_W_Cmd(SigMode,channel,0xBF);
            SSD2828_W_Data(SigMode,channel,0xED);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
            delay_ms(50);
            tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 												
            if (tmp&0x02)
                RM195_otptime=1;
        }		
    }
                                    
    buffer[4]=RM195_otptime;
    RM195_otptime=0x00;
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[3] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的OTP数据  ：ok  
}


void ProcessForIc2E( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
			case 0x01://写寄存器
				RM6719X_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x51://写51寄存器
				RM6719X_Write51_Register(OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;                            
			case 0x02://读寄存器
				RM6719X_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x08://写Gamma数据
				RM6719X_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09://读Gamma数据
				RM6719X_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x0C://otp time
			{
				RM195_otptime=USB_Rx_Buffer[4];
				USB_Rx_Buffer[4] = Uart_Error_None;
				STM2PC_RM671xx(pdev,CDC_IN_EP,USB_Rx_Buffer,USB_Rx_Buffer[2]+3);//返回 写Gamma状态  ：ok  
			}
			break;                            
			case 0x0A://otp page1
			case 0x0D://OTP page0
			case 0x0E://OTP page2
			case 0x0F://OTP page3 	
			case 0x07://SPR page5   
			case 0x17://CMD3_ANA			
			case 0x18://OTP_Page_D0 	
			case 0x19://OTP_Page_E0 
			case 0x1A://OTP_Page_10 	
			case 0x1B://OTP_Page_12 														
				RM6719X_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data,RM195_otptime);   break;
			case 0x0B://otp time read
				RM6719X_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;   
			case 0xDD://auto Gamma数据
				RM6719X_auto_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;														
			default:
				break;	
	}			
}


